<template>
    <form-container title="编辑" :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
        <a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
            <a-form-item label="编码" name="id">
                <a-input v-model:value="formState.id" allow-clear />
            </a-form-item>
            <a-form-item label="生产人员编码" name="employeeCode">
                <a-input v-model:value="formState.employeeCode" allow-clear />
            </a-form-item>
            <a-form-item label="生产人员名称" name="employeeName">
                <a-input v-model:value="formState.employeeName" allow-clear />
            </a-form-item>
            <a-form-item label="产线编码" name="lineCode">
                <a-select v-model:value="formState.lineCode" placeholder="请选择产线">
                    <a-select-option v-for="item in lines" :key="item.id" :value="item.id">{{ item.lineName
                        }}</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="性别" name="gender">
                <a-radio-group v-model:value="formState.gender">
                    <a-radio :value="1">男</a-radio>
                    <a-radio :value="2">女</a-radio>
                </a-radio-group>
            </a-form-item>
            <a-form-item label="工种类型" name="workType">
                <a-input v-model:value="formState.workType" placeholder="请输入名称" allow-clear />
            </a-form-item>
            <a-form-item label="班次组" name="shiftGroup">
                <a-select v-model:value="formState.shiftGroup" placeholder="请选择">
                    <a-select-option :value="1">早</a-select-option>
                    <a-select-option :value="2">中</a-select-option>
                    <a-select-option :value="3">晚</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="状态" name="status">
                <a-select v-model:value="formState.status" placeholder="请选择">
                    <a-select-option :value="1">启用</a-select-option>
                    <a-select-option :value="2">禁用</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="入职日期" name="hireDate">
                <a-date-picker v-model:value="formState.hireDate" show-time type="date" value-format="YYYY-DD-MM"
                    placeholder="Pick a date" style="width: 100%" />
            </a-form-item>
            <a-form-item label="备注信息" name="remark">
                <a-input v-model:value="formState.remark" placeholder="请输入名称" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
        </template>
    </form-container>
</template>

<script setup name="updatePassword">
import { required } from '@/utils/formRules'
import { message } from 'ant-design-vue'
import tool from '@/utils/tool'
import lineApi from '@/api/mes/basicdata/lineApi'
import employeeApi from '@/api/production/employeeApi'

// 定义emit事件
const emit = defineEmits({ successful: null })
// 默认是关闭状态
let visible = ref(false)
const formRef = ref()
// 表单数据
const formState = ref({})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (email) => {
    visible.value = true
    formState.value = email
}
// 关闭抽屉
const onClose = () => {
    emit('successful')
    visible.value = false
}
// 默认要校验的
const rules = {
    name: [required('请输入名称')]
}

// 提交数据
const onSubmit = async () => {
    formRef.value.validateFields().then(() => {
        submitLoading.value = true
        let fromData = {
            id: formState.value.id,
            employeeName: formState.value.employeeName,
            employeeCode: formState.value.employeeCode,
            lineCode: formState.value.lineCode,
            gender: formState.value.gender,
            workType: formState.value.workType,
            shiftGroup: formState.value.shiftGroup,
            status: formState.value.status,
            hireDate: formState.value.hireDate,
            remark: formState.value.remark,
        }
        // 提交数据
        employeeApi
            .updateEmployee(fromData)
            .then(() => {
                //formRef.value.resetFields()
                visible.value = false
                emit('successful', fromData)
            })
            .finally(() => {
                submitLoading.value = false
                callback() //调用组件中的回调方法
            })
    })
}

//产线
const lines = ref([])
//获取产线
const queryLines = () => {
    lineApi.lineSelect({}).then(res => {
        //console.log(res)
        lines.value = res
    })
    //lineApi.lineSelect()
}

onMounted(() => {
    queryLines()
})

//类型
const processTypeOptions = tool.dictList('PROCESS_TYPE')
// 报工模式
const processModeOptions = tool.dictList('PROCESS_MODE')

// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
    onOpen
})
</script>

<style scoped></style>
